using BasicData.API.Application.Command;
using BasicData.API.Application.Mediator;
using BasicData.API.Dtos;
using BasicData.ErrorCode;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Text;


    [ApiController]
    public class ExportController : ControllerBase
    {
       /// <summary>
       /// 谁家小孩呀
       /// </summary>
        private readonly IProductionInfoMediator _productionInfoMediator;

        public ExportController(IProductionInfoMediator productionInfoMediator)
        {
            _productionInfoMediator = productionInfoMediator;
        }

        /// <summary>
        /// 根据产品信息ID导出Excel
        /// </summary>
        /// <param name="id">产品信息ID</param>
        /// <returns>Excel文件</returns>
        [HttpGet("production/{id}")]
        public async Task<IActionResult> ExportProductionInfo(Guid id)
        {
            // 获取产品信息
            var command = new GetProductionInfoByIdCommand { Id = id };
            var result = await _productionInfoMediator.GetProductionInfoByIdAsync(command);
            
            if (result?.Data == null)
                return NotFound("未找到指定的产品信息");

            var data = result.Data;
            
            // 创建美化的 CSV 内容
            var csvContent = new StringBuilder();
            
            // 添加标题行
            csvContent.AppendLine("==========产品信息导出==========");
            csvContent.AppendLine($"导出时间：{DateTime.Now:yyyy年MM月dd日 HH:mm:ss}");
            csvContent.AppendLine(""); // 空行
            
            // 添加表头
            csvContent.AppendLine("字段名称,字段值,备注");
            
            // 添加数据行，格式化显示
            csvContent.AppendLine($"产品ID,\"{data.Id}\",系统唯一标识");
            csvContent.AppendLine($"生产批次,{data.BatchNumber},生产批次号");
            csvContent.AppendLine($"保质期,{data.ShelfLifeDays}天,产品保质期限");
            csvContent.AppendLine($"生产许可证号,{(string.IsNullOrEmpty(data.ProductionLicenseNumber) ? "暂无" : data.ProductionLicenseNumber)},企业生产资质");
            csvContent.AppendLine($"生产企业,{(string.IsNullOrEmpty(data.ManufacturerName) ? "暂无" : data.ManufacturerName)},制造商信息");
            csvContent.AppendLine($"关联商品ID,{data.GoodsId},商品系统编号");
            csvContent.AppendLine($"创建时间,{data.CreatedTime:yyyy年MM月dd日 HH:mm:ss},数据录入时间");
            
            // 添加底部信息
            csvContent.AppendLine(""); // 空行
            csvContent.AppendLine("==========导出完成==========");
            csvContent.AppendLine("注：此文件由系统自动生成，请妥善保存");
            
            // 使用 UTF-8 BOM 编码，确保中文正确显示
            var preamble = Encoding.UTF8.GetPreamble();
            var contentBytes = Encoding.UTF8.GetBytes(csvContent.ToString());
            var fileBytes = new byte[preamble.Length + contentBytes.Length];
            Array.Copy(preamble, 0, fileBytes, 0, preamble.Length);
            Array.Copy(contentBytes, 0, fileBytes, preamble.Length, contentBytes.Length);
            
            var fileName = $"产品信息导出_{data.BatchNumber}_{DateTime.Now:yyyyMMdd_HHmmss}.csv";
            
            return File(fileBytes, "text/csv; charset=utf-8", fileName);
        }
    }
 
